package com.hexacta.hrs.domain.staffing.assignment;

import java.io.Serializable;
import java.util.Calendar;

import org.vectrics.common.db.AbstractRecordObject;
import org.vectrics.employee.Employee;
import org.vectrics.recruiter.job.JobFunction;

import com.hexacta.hrs.domain.employee.history.CareerRole;
import com.hexacta.hrs.domain.staffing.ParticionablePorFecha;
import com.hexacta.hrs.domain.staffing.Project;

/**
 * Asignacion de un empleado a un proyecto.
 * 
 * @author ncortinez
 * 
 */
public class EmployeeAssignment extends AbstractRecordObject implements Serializable, Cloneable, ParticionablePorFecha {

    private static final long serialVersionUID = 8950674051815271333L;

    private Long id;

    // Es el rol que fue contratado. Puede no coincidir con el rol del empleado.
    private JobFunction assignedRole;

    private AssignmentType assignmentType;

    private Employee employee;

    private Project project;

    private double assignmentPorcentage;

    private String comments;

    private Calendar endDate;

    private Calendar startDate;

    private boolean tentative;

    public EmployeeAssignment() {
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    /**
     * GETTERS and SETTERS
     */

    public JobFunction getAssignedRole() {
        return assignedRole;
    }

    public void setAssignedRole(final JobFunction assignedRole) {
        this.assignedRole = assignedRole;
    }

    public AssignmentType getAssignmentType() {
        return assignmentType;
    }

    public void setAssignmentType(final AssignmentType assignmentType) {
        this.assignmentType = assignmentType;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(final Employee employee) {
        this.employee = employee;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(final Project project) {
        this.project = project;
    }

    public double getAssignmentPorcentage() {
        return assignmentPorcentage;
    }

    public void setAssignmentPorcentage(final double assignmentPorcentage) {
        this.assignmentPorcentage = assignmentPorcentage;
    }

    public double getFTE() {
        return employee.getHoursPerDay() * assignmentPorcentage / 8;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(final String comments) {
        this.comments = comments;
    }

    public Calendar getEndDate() {
        return endDate;
    }

    public void setEndDate(final Calendar endDate) {
        this.endDate = endDate;
    }

    public Calendar getStartDate() {
        return startDate;
    }

    public void setStartDate(final Calendar startDate) {
        this.startDate = startDate;
    }

    // Este getters debe existir para poder hacer el mappeo usando hibernatte.
    public boolean getTentative() {
        return isTentative();
    }

    public boolean isTentative() {
        return tentative;
    }

    public void setTentative(final boolean tentative) {
        this.tentative = tentative;
    }

    @Override
    public void setId(final Long id) {
        this.id = id;
    }

    @Override
    public Long getId() {
        return id;
    }

    public String getAvailability() {
        if (getAssignmentType().equals(AssignmentType.AddOnAvailable))
            return "Add On (Available)";
        if (getAssignmentType().equals(AssignmentType.AddOnNotAvailable))
            return "Add On (Not Available)";
        if (getAssignmentType().equals(AssignmentType.Assigned))
            return "Vendido";
        return "";
    }

    public AvailabilityStatus getAvailabilityStatus() {
        if (getAssignmentType() == null)
            return null;
        if (getAssignmentType().equals(AssignmentType.AddOnAvailable))
            return AvailabilityStatus.AddOnAvailable;
        if (getAssignmentType().equals(AssignmentType.AddOnNotAvailable))
            return AvailabilityStatus.AddOnNotAvailable;
        if (getAssignmentType().equals(AssignmentType.Assigned))
            if (getProject().isInternalProject())
                return AvailabilityStatus.Interno;
            else
                return AvailabilityStatus.AsignadoCliente;
        return null;
    }

    public JobFunction getEmployeeJobFunctionAtAssignmentDate() {
        CareerRole cr = getEmployee().getEmploymentConditionHistory().getCareerRoleInDate(startDate.getTime());
        if (cr == null)
            return null;
        return cr.getCareerRole();
    }

    public boolean isAddOnAvailable() {
        return assignmentType == AssignmentType.AddOnAvailable;
    }

    public boolean isAddOnNotAvailable() {
        return assignmentType == AssignmentType.AddOnNotAvailable;
    }
}
